use crate::domain::dto::{DictAddDTO, DictEditDTO, DictPageDTO, IdDTO};
use crate::domain::table::SysDict;
use crate::domain::vo::RespVO;
use crate::error_info;
use crate::service::CONTEXT;
use axum::response::IntoResponse;
use axum::Json;

#[utoipa::path(
    post,
    path = "/admin/sys_dict_page",
    request_body = DictPageDTO,
    responses(
        (status = 200, description = "success", body = IntoResponse),
        (status = 400, description = "fail", body = IntoResponse)
    )
)]
pub async fn page(page: Json<DictPageDTO>) -> impl IntoResponse {
    let data = CONTEXT.sys_dict_service.page(&page.0).await;
    RespVO::from_result(data).json()
}
#[utoipa::path(
    post,
    path = "/admin/sys_dict_add",
    request_body = DictAddDTO,
    responses(
        (status = 200, description = "success", body = IntoResponse),
        (status = 400, description = "fail", body = IntoResponse)
    )
)]
pub async fn add(mut arg: Json<DictAddDTO>) -> impl IntoResponse {
    if arg.name.is_none() {
        return RespVO::<u64>::from_error(error_info!("empty")).json();
    }
    if arg.code.is_none() {
        return RespVO::<u64>::from_error(error_info!("empty")).json();
    }
    if arg.state.is_none() {
        arg.state = Some(1);
    }
    let res = SysDict::from(arg.0);
    let data = CONTEXT.sys_dict_service.add(&res).await;
    let _ = CONTEXT.sys_dict_service.update_cache().await;
    RespVO::from_result(data).json()
}

#[utoipa::path(
    post,
    path = "/admin/sys_dict_update",
    request_body = DictEditDTO,
    responses(
        (status = 200, description = "success", body = IntoResponse),
        (status = 400, description = "fail", body = IntoResponse)
    )
)]
pub async fn update(arg: Json<DictEditDTO>) -> impl IntoResponse {
    let data = CONTEXT.sys_dict_service.edit(&arg.0).await;
    let _ = CONTEXT.sys_dict_service.update_cache().await;
    RespVO::from_result(data).json()
}

#[utoipa::path(
    post,
    path = "/admin/sys_dict_remove",
    request_body = IdDTO,
    responses(
        (status = 200, description = "success", body = IntoResponse),
        (status = 400, description = "fail", body = IntoResponse)
    )
)]
pub async fn remove(arg: Json<IdDTO>) -> impl IntoResponse {
    let data = CONTEXT
        .sys_dict_service
        .remove(&arg.0.id.unwrap_or_default())
        .await;
    let _ = CONTEXT.sys_dict_service.update_cache().await;
    RespVO::from_result(data).json()
}
